package timeline;

import java.io.*;

public class WGet {
    
	private static void close(Closeable c) {
	    if (c != null) {
	      try {
	        c.close();
	      } catch (IOException e) {
	        // ignored
	      }
	    }
	}
        public static void main(String[] args) {

        String cmd = "wget -r -l1 ";
    	String url = "www.googlc.com";
    	String dir = "wget/t1/";
    	String logfile = dir+"wget.log";
    	
    	String cmdLine = cmd+" -P " +dir+" -o " +logfile+" "+url;
    	try{
    		
    	boolean success = (new File(dir)).mkdir();
    	if (!success) 
     	  { 
    	      System.out.println("Error: Directory: " + dir + "can not be created");
    	  }    
    	}
    	catch(Exception exception) {exception.printStackTrace();}
    	  
    	Process proc = null;
    	    try {
    	      ProcessBuilder pb = new ProcessBuilder(cmdLine);
    	      proc = pb.start();
    	      proc.waitFor();
    	    }
    	    catch(Exception exception) {exception.printStackTrace();}
    	    finally {
    	      if (proc != null) {
    	        close(proc.getOutputStream());
    	        close(proc.getInputStream());
    	        close(proc.getErrorStream());
    	        proc.destroy();
    	      }
    	    }

    	    
/*    	BufferedReader stdInput = new BufferedReader(new 
                InputStreamReader(p.getInputStream()));

           BufferedReader stdError = new BufferedReader(new 
                InputStreamReader(p.getErrorStream()));

           // read the output from the command
           
          System.out.println("Here is the standard output of the command:\n");
           while ((s = stdInput.readLine()) != null) {
               System.out.println(s);
           }
           
           // read any errors from the attempted command

           System.out.println("Here is the standard error of the command (if any):\n");
           while ((s = stdError.readLine()) != null) {
               System.out.println(s);
           }

    	}*/
    	
    			
        }
}
